package com.avic.rong.questionOrAnswer.fragment;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.avic.rong.BaseFragment;
import com.avic.rong.R;
import com.avic.rong.questionOrAnswer.activity.PersonalHomeActivity;
import com.avic.rong.questionOrAnswer.adapter.TrainListAdapter;
import com.avic.rong.questionOrAnswer.api.RrhStudyApi;
import com.avic.rong.questionOrAnswer.bean.TrainBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class TrainFragment extends BaseFragment {


    @BindView(R.id.recy_list)
    RecyclerView recyList;
    @BindView(R.id.loading_layout)
    LoadingLayout loading_layout;
    Unbinder unbinder;

    Unbinder unbinder1;
    private List<TrainBean.CoursesListBean> listData = new ArrayList<>();
    private TrainListAdapter trainListAdapter;
    public static String PIC = "logo";
    private String homeUserID;

    @Override
    protected void initView() {

        Bundle arguments = getArguments();
        homeUserID = arguments.getString("homeUserID");

        loading_layout.setStatus(LoadingLayout.LOADING);

        recyList.setLayoutManager(new LinearLayoutManager(getContext()));
        trainListAdapter = new TrainListAdapter(getContext(), listData);
        recyList.setAdapter(trainListAdapter);
        initData();
    }

    private void initData() {

        Map<String, String> map = new HashMap<>();
        if (!getSharedPreferencesUtil().getLogintoken().isEmpty()) {
            map.put("token", getSharedPreferencesUtil().getLogintoken());
        } else {
            map.put("token", "");
        }

        map.put("pageNo", currentPageNumber + "");
        map.put("pageSize", "20");
        if (TextUtils.isEmpty(homeUserID)) {
            map.put("userId", "");
        } else {
            map.put("userId", homeUserID);
        }
        final RrhStudyApi rrhStudyApi = new RrhStudyApi((PersonalHomeActivity) getContext(), simpleOnNextListener);
        rrhStudyApi.setCache(false);
        rrhStudyApi.setMap(map);
        HttpManager.getInstance().doHttpDeal(rrhStudyApi);

        loading_layout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                HttpManager.getInstance().doHttpDeal(rrhStudyApi);
                loading_layout.setStatus(LoadingLayout.LOADING);
            }

            @Override
            public void onReturn(View v) {
                getActivity().onBackPressed();
            }
        });
       /* } else {
            loading_layout.setStatus(LoadingLayout.EMPTY);
        }*/
    }

    @Override
    protected void addListener() {

    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_train;
    }

    //培训列表
    HttpOnNextListener simpleOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (isRefresh) {
                listData.clear();
            }
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(getContext(), subjects.getMsg());
                return;
            }
            if (isRefresh) {
                ((PersonalHomeActivity)getContext()).refreshLayout.finishRefresh();
            } else {
                ((PersonalHomeActivity)getContext()).refreshLayout.finishLoadMore();
            }
            loading_layout.setStatus(LoadingLayout.SUCCESS);
            TrainBean data = (TrainBean) subjects.getResult();
            List<TrainBean.CoursesListBean> coursesList = data.getCoursesList();
            if (coursesList != null) {

                listData.addAll(coursesList);
                if (listData != null && listData.size() <= 0) {
                    loading_layout.setStatus(LoadingLayout.EMPTY);
                }
                trainListAdapter.setFileServer(data.getFileServer());
                trainListAdapter.notifyDataSetChanged();
            }

        }


        @Override
        public void onCacheNext(String cache) {


        }


        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", e.getMessage());
            if (e instanceof SocketTimeoutException || e instanceof ConnectException) {
                loading_layout.setStatus(LoadingLayout.NO_NETWORK);
            } else {
                loading_layout.setStatus(LoadingLayout.ERROR);
            }
        }
    };



    public void onLoadMore() {
        isRefresh = false;
        currentPageNumber = currentPageNumber + 1;
        initData();
    }


    public void onRefresh() {
        isRefresh = true;
        currentPageNumber = 1;
        initData();
    }
}
